﻿using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;
using Web.Account;

namespace AssetManage.Handler
{
    public class Valid : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext currentContext)
        {
            var section = currentContext.Request.Params["section"];
            var userid = currentContext.Request.Params["userid"];
            var tableid = currentContext.Request.Params["tableid"];
            var captcha = currentContext.Request.Params["captcha"];
            var email = currentContext.Request.Params["email"];
            var emailcaptcha = currentContext.Request.Params["emailcaptcha"];

            var responseJsonStringBuilder = new StringBuilder();
            var responseJsonStringWriter = new StringWriter(responseJsonStringBuilder);

            var responseJsonWriter = new JsonTextWriter(responseJsonStringWriter) {Formatting = Formatting.Indented};
            responseJsonWriter.WriteStartObject();

            switch (section)
            {
                case "userid":
                    responseJsonWriter.WritePropertyName("exist");
                    responseJsonWriter.WriteValue(AccountManage.IsExistUserID(userid));
                    break;
                case "captcha":
                    if (currentContext.Request.Cookies["ASP.NET_SessionId"] == null)
                    {
                        responseJsonWriter.WritePropertyName("success");
                        responseJsonWriter.WriteValue(false);
                        responseJsonWriter.WritePropertyName("error");
                        responseJsonWriter.WriteValue("Cookie_Error");
                        break;
                    }
                    if (currentContext.Session["CAPTCHA"] != null && !String.IsNullOrWhiteSpace(captcha) &&
                        String.Equals(currentContext.Session["CAPTCHA"].ToString(), captcha,
                            StringComparison.CurrentCultureIgnoreCase))
                    {
                        responseJsonWriter.WritePropertyName("success");
                        responseJsonWriter.WriteValue(true);
                    }
                    else
                    {
                        responseJsonWriter.WritePropertyName("success");
                        responseJsonWriter.WriteValue(false);
                        responseJsonWriter.WritePropertyName("error");
                        responseJsonWriter.WriteValue("Captcha_Error");
                    }
                    break;
                case "tableid":
                    responseJsonWriter.WritePropertyName("exist");
                    responseJsonWriter.WriteValue(AccountManage.IsExistTableID(tableid));
                    break;
                case "email":
                    responseJsonWriter.WritePropertyName("exist");
                    responseJsonWriter.WriteValue(AccountManage.IsExistEmail(email));
                    break;
                case "emailcaptcha":
                    if (currentContext.Request.Cookies["ASP.NET_SessionId"] == null)
                    {
                        responseJsonWriter.WritePropertyName("success");
                        responseJsonWriter.WriteValue(false);
                        responseJsonWriter.WritePropertyName("error");
                        responseJsonWriter.WriteValue("Cookie_Error");
                    }
                    if (!String.IsNullOrEmpty((String) currentContext.Session["EMAIL_CAPTCHA"]) &&
                        !String.IsNullOrEmpty(emailcaptcha) &&
                        currentContext.Session["EMAIL_CAPTCHA"].ToString() == emailcaptcha)
                    {
                        responseJsonWriter.WritePropertyName("success");
                        responseJsonWriter.WriteValue(true);
                    }
                    else
                    {
                        responseJsonWriter.WritePropertyName("success");
                        responseJsonWriter.WriteValue(false);
                        responseJsonWriter.WritePropertyName("error");
                        responseJsonWriter.WriteValue("EmailCaptcha_Error");
                    }
                    break;
            }
            responseJsonWriter.WriteEndObject();
            currentContext.Response.Write(responseJsonStringBuilder.ToString());
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
}